package com.qf.controller;

import com.qf.pojo.Application;
import com.qf.pojo.Customer;
import com.qf.service.ApplicationService;
import com.qf.service.CustomerService;
import com.qf.utils.AjaxMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：冯国清
 * 项目名：com.qf.controller
 * 时间：2020/7/29 21:19
 * 描述:
 */
@Controller
public class ApplicationController {
    @Resource
    private ApplicationService applicationService;
    @RequestMapping("/application")
    @ResponseBody
    public Map<String,Object> getAllApplication(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "5")int limit){
        Map<String, Object> map  = null;
        List<Object> list = new ArrayList<>();
        List<Application> allApplication= applicationService.getAllApplication(page, limit);
        /*private int aid;
private String acorpName;
private String aappName;
private String aappKey;
private String aAppSecret;
private String aredirectUrl;
private int alint;
private String adescription;
private int acusid;
private int astate;*/
        for (Application c : allApplication) {
            map = new HashMap<>();
            map.put("aid", c.getAid());
            map.put("acorpName", c.getAcorpName());
            map.put("aappName", c.getAappName());
            map.put("aappKey", c.getAappKey());
            map.put("aaa",c.getAaa());
            map.put("aredirectUrl", c.getAredirectUrl());
            map.put("alinit", c.getAlinit());
            map.put("adescription", c.getAdescription());
            map.put("acusid", c.getAcusid());
            if (1 == c.getAstate()) {
                map.put("astate", "正常");
            } else {
                map.put("astate", "停用");
            }
            list.add(map);
        }
        Map<String, Object> applicationMap = new HashMap<>();
       applicationMap.put("code", 0);
        applicationMap.put("msg", "");
        applicationMap.put("count", applicationService.getCount());
        applicationMap.put("data", list);
        return applicationMap;
    }
    @ResponseBody
    @RequestMapping("/addAppliction")
    public String saveApplication(Application application){
        int i = applicationService.saveApplication(application);
        if (i>=1){
            return "success";
        }else {
            return "failed";
        }
    }
    @RequestMapping("/selectApplicationById1/{aid}")
    public String selectApplicationById1(@PathVariable int aid, Model model){
        Application application = applicationService.selectApplicationById(aid);
        model.addAttribute("application",application);

        System.out.println(1111);
        System.out.println(application);
        System.out.println(1111);

        return "/ShowApplication1.jsp";
    }
    @RequestMapping("/selectApplicationById2/{aid}")
    public String selectApplicationById2(@PathVariable int aid,Model model){
        Application application = applicationService.selectApplicationById(aid);
        System.out.println(application);
        model.addAttribute("application",application);
        return "/UpdateApplication.jsp";
    }
    @RequestMapping("/selectById/{aid}")
    public Application selectCustomerById(@PathVariable int aid,Model model){
        Application application = applicationService.selectApplicationById(aid);
        return application;
    }
    @ResponseBody
    @RequestMapping("/updateApplication")
    public String updateApplication(Application application){
        int i = applicationService.updateApplication(application);
        if (i>=1){
            return "success";
        }else {
            return "failed";
        }
    }
    @RequestMapping("/deleteApplication/{aid}")
    @ResponseBody
    public String deleteApplication(@PathVariable int aid){
        System.out.println(aid);
        int i = applicationService.deleteApplication(aid);
        if (i>=1){
            return "success";
        }
        return "failed";
    }
    @RequestMapping("/deleteApplication1")
    @ResponseBody
    public AjaxMessage delete(@RequestParam("aids")int[] aids){

        if(aids!=null&&aids.length>0){
           applicationService.deleteByIds(aids);
            return new AjaxMessage(true,"删除成功");
        }
        return new AjaxMessage(false,"删除失败");
    }

}


